/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.rofl.units;

import dk.rofl.graphics.GameTileSet;
import dk.rofl.items.Equipment;
import dk.rofl.items.GameChest;
import dk.rofl.items.GameItem;
import java.awt.image.BufferedImage;
import rofl.Utils;

/**
 *
 * @author Jeppe
 */
public abstract class Monster extends GameUnit {

    public static final int GOBLIN = 0;
    
    public static final int TYPE_WEAK = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_STRONG = 2;
    public static final int TYPE_ELITE = 3;
    public static final int TYPE_BOSS = 4;
    private static GameTileSet tileSet;
    private String name;
    private int mobType;
    private int monster;
    
    public Monster(int level, int monster, String name, int mobType) {
        super(null, level);
        this.monster = monster;
        this.name = name;
        this.mobType = mobType;
    }

    public static void setTileSet(GameTileSet gts) {
        tileSet = gts;
    }
    
    // Get experience dropped by Monster.
    public int getExperience() {
        int xp = getLevel() * 50 + Utils.rnd().nextInt(getLevel() * 25);

        switch (mobType) {
            case Monster.TYPE_NORMAL:
                xp = xp * 2;
                break;
            case Monster.TYPE_STRONG:
                xp = xp * 4;
                break;
            case Monster.TYPE_ELITE:
                xp = xp * 6;
                break;
            case Monster.TYPE_BOSS:
                xp = xp * 10;
                break;
        }
        return xp;
    }

    // Get a GameChest reward from this monster.
    public GameChest getChestReward() {
        GameChest gc = new GameChest();

        gc.setGoldAmount(getLevel() + Utils.rnd().nextInt(getLevel()));

        int ql = Equipment.generateItemQuality();
        switch (mobType) {
            case TYPE_WEAK:
                while (ql > Equipment.QUALITY_MAGIC) {
                    ql = Equipment.generateItemQuality();
                }
                break;
            case TYPE_NORMAL:
                while (ql < Equipment.QUALITY_NORMAL || ql > Equipment.QUALITY_SUPERIOR) {
                    ql = Equipment.generateItemQuality();
                }
                break;
            case TYPE_STRONG:
                while (ql < Equipment.QUALITY_MAGIC) {
                    ql = Equipment.generateItemQuality();
                }
                break;
            case TYPE_ELITE:
                while (ql < Equipment.QUALITY_SUPERIOR) {
                    ql = Equipment.generateItemQuality();
                }
                break;
            case TYPE_BOSS:
                while (ql < Equipment.QUALITY_EPIC) {
                    ql = Equipment.generateItemQuality();
                }
                break;
        }
        int it = GameItem.generateItemType();
        if (mobType < TYPE_STRONG) {
            gc.addItem(GameItem.generateItem(it, getLevel(), ql));
        } else {
            while (it == GameItem.TYPE_POTION) {
                it = GameItem.generateItemType();
            }
            gc.addItem(GameItem.generateItem(it, getLevel(), ql));
            it = GameItem.generateItemType();
            gc.addItem(GameItem.generateItem(it, getLevel(), Equipment.generateItemQuality()));
        }
        return gc;
    }

    // Get the name of this monster.
    public String getName() {
        String mName = name;

        switch (mobType) {
            case Monster.TYPE_WEAK:
                mName = "Weak " + name;
                break;
            case Monster.TYPE_NORMAL:
                mName = name;
                break;
            case Monster.TYPE_STRONG:
                mName = "Strong " + name;
                break;
            case Monster.TYPE_ELITE:
                mName = name + " (Elite)";
                break;
            case Monster.TYPE_BOSS:
                mName = name + " (Boss)";
                break;
        }
        return mName;
    }

    // Set the name of this monster.
    public void setName(String name) {
        this.name = name;
    }

    // Get the type of this monster.
    public int getMobType() {
        return mobType;
    }

    // Promote this monster to the next level.
    @Override
    public void nextLevel() {
        super.nextLevel();
        int sta = 3 - Utils.rnd().nextInt(2) + 1;
        addBaseStamina(3 + sta);
        addBaseStrength(3 + (5 - sta));
        setCurrentHealth(getMaxHealth());
    }

    // Generate a monster from some specific types.
    public static Monster generateMonster(int gameLevel, int mobType) {
        Monster m = null;
        int r = Utils.rnd().nextInt(1);
        switch (r) {
            case 0:
                int lvl = Utils.rnd().nextInt(2) + gameLevel;
                if (lvl < 1) {
                    lvl = 1;
                }
                m = new GoblinMonster(lvl, mobType);
                break;
        }
        return m;
    }
    
    @Override
    public BufferedImage getImage() {
        int index;
        if(isDead()) {
            index = monster * 2 + IMG_DEAD;
        } else {
            index = monster * 2;
        }
        BufferedImage img = tileSet.getTileImage(index);
        return img;
    }
}
